package gui_layer;

import java.awt.Color;
import java.awt.Font;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JOptionPane;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

import model_layer.Sale;
import ctr_layer.SaleCtr;

import javax.swing.JButton;

@SuppressWarnings("serial")
public class ManageSaleGUI extends SaleRestockManageWindow
{

	protected String[] column_names = { "id", "Date", "Customer", "Employee", "Total price" };
	private SaleCtr sale_ctr;

	/**
	 * Create the frame.
	 */
	public ManageSaleGUI()
	{
		sale_ctr = new SaleCtr();
		setTitle("Manage Sales");
		setBounds(100, 100, 450, 300);
		make_invoice_btn();
		prepare_table(new Object[0][5], column_names);
		table.removeColumn(table.getColumnModel().getColumn(0));
		fill_table();
		
		this.addInternalFrameListener(new InternalFrameAdapter()
		{		
			@Override
			public void internalFrameActivated(InternalFrameEvent arg0)
			{
				refresh_table();
			}
		});

	}
	
	private void make_invoice_btn()
	{
		float[] values = Color.RGBtoHSB(255, 104, 0, null);
		JButton btnPrintInvoice = new JButton("Print Invoice");
		btnPrintInvoice.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		btnPrintInvoice.setForeground(Color.WHITE);
		btnPrintInvoice.setBackground(Color.getHSBColor(values[0], values[1], values[2]));
		btnPrintInvoice.setContentAreaFilled(false);
		btnPrintInvoice.setOpaque(true);
		btnPrintInvoice.setBorder(new RoundedCornerBorder());
		btnPrintInvoice.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				print_invoice();
				
			}
		});
		additional_button_panel.add(btnPrintInvoice);
	}
	
	private void refresh_table()
	{
		MyTableModel mtm = (MyTableModel) table.getModel();
		mtm.setDataVector(new Object[0][5], column_names);
		fill_table();
		table.removeColumn(table.getColumnModel().getColumn(0));
		table.repaint();
	}
	
	private void fill_table()
	{
		List<Sale> sales = sale_ctr.find_all_sales();
		Object[] data;
		MyTableModel mtm = (MyTableModel) table.getModel();
		for(Sale sale : sales)
		{
			data = new Object[5];
			data[0] = sale.getId();
			data[1] = GUIUtilities.convert_date_to_string(sale.getSale_date());
			data[2] = sale.getCustomer().getName();
			data[3] = sale.getStaff_member().getFname() + " " + sale.getStaff_member().getLname();
			data[4] = sale.getTotal_price();
			mtm.addRow(data);
		}
	}

	@Override
	protected void create_new()
	{
		NewSaleGUI ns_gui = new NewSaleGUI(null);
		
		this.getDesktopPane().add(ns_gui);
		Point window_corner = this.getLocation();
		ns_gui.setLocation((int) window_corner.getX() + 15, (int) window_corner.getY() + 15);
		ns_gui.setVisible(true);
		ns_gui.addInternalFrameListener(new InternalFrameAdapter()
		{		
			@Override
			public void internalFrameClosing(InternalFrameEvent arg0)
			{
				refresh_table();
				
			}
		});
	}

	@Override
	protected void details()
	{
		int row = table.getSelectedRow();
		if(row != -1)
		{
			row = table.convertRowIndexToModel(row);
			Sale sale = sale_ctr.find_sale((int) table.getModel().getValueAt(row, 0));
			if(sale != null)
			{
				NewSaleGUI ns_qui = new NewSaleGUI(sale);
				this.getDesktopPane().add(ns_qui);
				Point window_corner = this.getLocation();
				ns_qui.setLocation((int) window_corner.getX() + 15, (int) window_corner.getY() + 15);
				ns_qui.setVisible(true);
			}
			else
			{
				refresh_table();
			}
		}
	}

	@Override
	protected void delete()
	{
		int row = table.getSelectedRow();
		if(row != -1)
		{
			row = table.convertRowIndexToModel(row);
			if(JOptionPane.showConfirmDialog(this, "Are you sure you wish to delete this sale? \n"
						+ "This action cannot be undone.", "Delete Sale", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
			{
				if(sale_ctr.delete_sale((int) table.getModel().getValueAt(row, 0)))
				{
					JOptionPane.showMessageDialog(this, "Sale successfully deleted!", "Success", JOptionPane.PLAIN_MESSAGE);
				}
				else
				{
					JOptionPane.showMessageDialog(this, "Sale was not deleted!", "Error", JOptionPane.ERROR_MESSAGE);
				}
			}
			refresh_table();
		}
	}
	
	private void print_invoice()
	{
		int row = table.getSelectedRow();
		if(row != -1)
		{
			row = table.convertRowIndexToModel(row);
			if(JOptionPane.showConfirmDialog(this, "Are you sure you wish to print an invoice? \n", "Print invoice",
					JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
			{
				InvoicePrinter ip = new InvoicePrinter(sale_ctr.find_sale((int)table.getModel().getValueAt(row, 0)));
				ip.print();
			}
		}
	}

}
